//
//  ZdshpbVC.swift
//  Auscultation
//
//  Created by 来 on 2020/12/9.
//

import UIKit

class ZdshpbVC: BaseVC {

    var scrollView: UIScrollView!
    
    var ccsl: LGFLoginImageTitleView!
    var ybsl: LGFLoginImageTitleView!
    var shlx: LGFLoginImageTitleView!
    var ybgz: LGFLoginImageTitleView!
    var rygz: LGFLoginImageTitleView!
    
    var kbzyb: UILabel!
    var kbzyb2: UILabel!
    var kbzyb_i: Int!
    var kbzyb2_i: Int!
    var kbzry: UILabel!
    var kbzry_i: Int!

    override func viewDidLoad() {
        super.viewDidLoad()

        initUI()
        getData()
    }
    
    func getData() {
        let paramet1: [String: Any] = [
            "pageSize": 9999,
            "page": 1,
            "isDeleted": 1,
            "roleIds": ["3124297241460736"],
            "hospitalId": EWEUser.lgf.hospitalId ?? "",
            "scope": "2",
        ]
        EWERequest.lgf("user", paramet1, .get) { [weak self] (data) in
            self?.kbzry.text = "可审核人员：" + ((data["totalItems"] as? Int) ?? 0).description
            self?.kbzry_i = (data["totalItems"] as? Int) ?? 0
        } _: { (error) in
            SVProgressHUD.showError(error)
        }
        // 待审核样本
//        let paramet2: [String: Any] = [
//            "pageSize": 9999,
//            "page": 1,
//            "isDeleted": 1,
//            "batch": 0,
//            "hospitalId": EWEUser.lgf.hospitalId ?? ""
//        ]
//        EWERequest.lgf("same", paramet2, .get) { [weak self] (data) in
//            let num = ((data["totalItems"] as? Int) ?? 0).description
//            self?.kbzyb.text = "可审核样本：" + num
//            self?.kbzyb_i = Int(num)
//        } _: { (error) in
//            SVProgressHUD.showError(error)
//        }
        // 疑难样本
        let paramet3: [String: Any] = [
            "pageSize": 9999,
            "page": 1,
            "auditStatus": 0,
            "labelStatus": 20,
            "roleId": "3124295500824576"
        ]
        EWERequest.lgf("difficult", paramet3, .get) { [weak self] (data) in
            let num = ((data["totalItems"] as? Int) ?? 0).description
            self?.kbzyb2.text = "可审核样本：" + num
            self?.kbzyb2_i = Int(num)
        } _: { (error) in
            SVProgressHUD.showError(error)
        }
    }
    
    @objc func queding(_ sender: UIButton) {
        if shlx.textField.text!.lgf_IsBlank() {
            SVProgressHUD.showError("请选择派包类型")
            return
        }
        if self.shlx.lgf_Name == "20" {
            if ybsl.textField.text!.lgf_IsBlank() {
                SVProgressHUD.showError("请填写样本数量")
                return
            }
            if (Int(ybsl.textField.text!) ?? 0) > self.kbzyb2_i {
                SVProgressHUD.showError("样本数量不可大于" + self.kbzyb2_i.description)
                return
            }
            if ybgz.textField.text!.lgf_IsBlank() {
                SVProgressHUD.showError("请选择样本规则")
                return
            }
        } else {
            if ybsl.textField.text!.lgf_IsBlank() {
                SVProgressHUD.showError("请填写样本数量")
                return
            }
            if (Int(ybsl.textField.text!) ?? 0) > self.kbzyb_i {
                SVProgressHUD.showError("样本数量不可大于" + self.kbzyb_i.description)
                return
            }
            if ccsl.textField.text!.lgf_IsBlank() {
                SVProgressHUD.showError("请填写抽查数量")
                return
            }
            if (Int(ccsl.textField.text!) ?? 0) > 30 {
                SVProgressHUD.showError("抽查数量最多30个")
                return
            }
            if ybgz.textField.text!.lgf_IsBlank() {
                SVProgressHUD.showError("请选择样本规则")
                return
            }
        }
        
        if rygz.textField.text!.lgf_IsBlank() {
            SVProgressHUD.showError("请选择人员规则")
            return
        }
        let paramet: [String: String] = [
            "hospitalId": EWEUser.lgf.hospitalId,
            "type": shlx.lgf_Name ?? "",
            "sampleTotal": ybsl.textField.text ?? "",
            "sampleRule": ybgz.lgf_Name ?? "",
            "userRule": rygz.lgf_Name ?? "",
            "checkNum": ccsl.textField.text ?? "",
            "auditUser": EWEUser.lgf.userId
        ]
        EWERequest.lgf("audit?rule=1", paramet, .post, JSONEncoding.default) { [weak self] (data) in
            SVProgressHUD.showSuccess("派包成功")
            NotificationCenter.default.post(name: NSNotification.Name(rawValue: "ReloadShrwbVC"), object: nil)
            Timer.lgf_After(S: 0.5) {
                self?.navigationController?.popViewController(animated: true)
            }
        } _: { (error) in
            SVProgressHUD.showError(error)
        }
    }

}
